//
//  JXUserModel.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/2/26.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit

class JXUserModel: NSObject,NSCoding{

    ///access_token	string	用于调用access_token，接口获取授权后的access token。\
    var access_token: String?
    
    
    ///expires_in	string	access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0{
        didSet{
//            expiresdate = NSDate(timeIntervalSinceNow: expires_in)
            expiresdate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    


    ///remind_in	string	access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
    ///uid	string	当前授权用户的UID。
    var uid: String?
    
    //再这个类的基础上添加2个属性
    ///profile_image_url	string	用户头像地址（中图），50×50像素
    var profile_image_url: String?
    ///screen_name	string	用户昵称
    var screen_name: String?
    // 定义日期
    var expiresdate: NSDate?
    
//    override init() {
//        super.init()
//        
//    }
    
  
    // kvc 构造函数
    init (dic: [String:NSObject]){
        
        super.init()
        
        setValuesForKeysWithDictionary(dic)
    }
    
    // 防止KVC 崩溃
//    override func setValue(value: AnyObject?, forKey key: String) {}
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // 解档 归档  路径
//    private static let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathExtension("rues.pdf")!
    
    // 保存
            // 沙盒路径
    func save(file: String){
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
            // 拼接文件路径
        let filePath = (path as NSString).stringByAppendingPathComponent(file)
        // 归档
      NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
       }
    
    // 归档
    func encodeWithCoder(aCoder: NSCoder){
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresdate, forKey: "expiresdate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
      
    }
    // 解档
   required init?(coder aDecoder:NSCoder){
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
    
//    self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSData
    
    self.expiresdate = aDecoder.decodeObjectForKey("expiresdate") as? NSDate
    
    self.uid = aDecoder.decodeObjectForKey("uid") as? String
    
    self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    
    self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url")as? String
    }
    
    // 解析当前对象
    class func loadUserModel(file: String) -> JXUserModel? {
        
//        // 路径
//        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
//        let filePath = (path as NSString).stringByAppendingPathComponent(file)
        // 路径
        let path =  NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        let filePath = (path as NSString).stringByAppendingPathComponent(file)
        
        // 解档获取当前的用户数据
        let model = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? JXUserModel
        
        if let date = model?.expiresdate{
            
             if date.compare(NSDate()) == .OrderedDescending{
               
                return model
            }else{
                return nil
            }
            
        }
            return nil
    }
}

